<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>找回密码</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="Keywords" content="网站关键词">
    <meta name="Description" content="网站介绍">
    <link rel="stylesheet" href="../../static/css/base.css">
    <link rel="stylesheet" href="../../static/css/iconfont.css">
    <link rel="stylesheet" href="../../static/css/reg.css">
    <link rel="shortcut icon" href="../../static/img/favicon.ico">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="ajax-hook"></div>
<div class="wrap">
    <div class="wpn">
        <div class="form-data find_password">
            <h4>找回密码</h4>
            <p class="right_now">已有账号，<a href="/index/login">马上登录</a></p>
            <p class="p-input pos">
                <label for="pc_email">邮箱</label>
                <input type="text" id="pc_email">
                <span class="tel-warn pc_tel-err hide"><em>最多五个字</em><i class="icon-warn"></i></span>
            </p>
            <p class="p-input pos">
                <label for="veri-code">输入验证码</label>
                <input type="number" id="veri-code">
                <button onclick="sendEmail()" class="send">发送验证码</button>
                <span class="tel-warn error hide"><em>验证码错误，请重新输入</em><i class="icon-warn"></i></span>
            </p>
            <p class="p-input pos pwd hide" id="pwd">
                <label for="passport">输入密码</label>
                <input type="password" id="password" style="display: none"/>
                <input type="password" id="passport" name="password">
                <span class="tel-warn pwd-err hide"><em></em><i class="icon-warn" style="margin-left: 5px"></i></span>
            </p>
            <p class="p-input pos confirmpwd hide" id="confirmpwd">
                <label for="passport2">确认密码</label>
                <input type="password" style="position:absolute;top:-998px"/>
                <input type="password" id="passport2">
                <span class="tel-warn confirmpwd-err hide"><em></em><i class="icon-warn" style="margin-left: 5px"></i></span>
            </p>
            <button onclick="getNext()" class="lang-btn next">下一步</button>
            <button onclick="resetPass()" class="lang-btn reset hide">重置密码</button>
            <p class="right">Powered by © 2019</p>
        </div>
    </div>
</div>
<script>
    function getNext() {
        var code = $.trim($('#veri-code').val());
        $.ajax({
            url: '/user/checkCode',
            type: 'post',
            dataType: 'json',
            async: true,
            data: {code:code},
            success: function (data) {
                console.log(data)
                if (data.status == 500){
                    if (data.msg == "1"){
                        $('.error').removeClass('hide').find("em").text('请发送验证码');
                    } else if (data.msg == "2"){
                        $('.error').removeClass('hide').find("em").text('验证码错误');
                    }
                } else if (data.status == 200){
                    $('.pwd').removeClass('hide');
                    $('.confirmpwd').removeClass('hide');
                    $('.next').addClass('hide')
                    $('.reset').removeClass('hide')
                    $('.error').addClass('hide');
                }
            }
        });
    }

    function sendEmail() {
        var email = $.trim($('#pc_email').val());
        if (checkAccount(email)) {
            var ldata = {to: email};
            $.ajax({
                url: '/user/reset',
                type: 'post',
                dataType: 'json',
                async: true,
                data: ldata,
                success: function (data) {
                    console.log(data)
                    if (data.status == 200){
                        alert("验证码发送成功")
                    }
                }
            });
        } else {
            return false;
        }
    }

    function resetPass() {
        var email = $.trim($('#pc_email').val());
        var password = $.trim($('#passport').val());
        var password2 = $.trim($('#passport2').val());
        if (checkAccount(email) && checkPass(password,password2)) {
            var ldata = {email: email,password:password};
            $.ajax({
                url: '/user/resetPassword',
                type: 'post',
                dataType: 'json',
                async: true,
                data: ldata,
                success: function (data) {
                    console.log(data)
                    if (data.status == 200){
                        alert("重置密码成功")
                    }else if (data.status == 500){
                        $('.pc_tel-err').removeClass('hide').find("em").text('邮箱不存在');
                    }
                }
            });
        } else {
            return false;
        }
    }

    function checkAccount(username){
        var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if (username == '') {
            $('.pc_tel-err').removeClass('hide').find("em").text('请输入邮箱');
            return false;
        }else if (!pattern.test(username)){
            $('.pc_tel-err').removeClass('hide').find("em").text('邮箱非法');
            return false;
        }
        else {
            $('.pc_tel-err').addClass('hide');
            return true;
        }
    }
    function checkPass(pass1,pass2){
        if (pass1 == '') {
            $('.pwd-err').removeClass('hide').text('请输入密码');
            return false;
        }else if (pass1 != pass2){
            $('.confirmpwd-err').removeClass('hide').text('两次密码不一致');
        } else {
            $('.pwd-err').addClass('hide');
            $('.confirmpwd-err').addClass('hide');
            return true;
        }
    }
</script>
<script src="../../static/js/jquery.js"></script>
<script src="../../static/js/agree.js"></script>

</body>
</html>